<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
/**
 * 列表控制器
 */
class ListController extends Controller
{
	/**
	 * 列表首页
	 */
	public function index()
	{
		$cid = Q('get.cid',0,'intval');
		//处理面包屑
//		p(Db::table('category')->get());
        $fatherData = $this->getFather(Db::table('category')->get(), $cid);
//		p($fatherData);
        //翻转数组
        $fatherData = array_reverse($fatherData);
		//p($fatherData);
		//分配数据
		View::with('fatherData',$fatherData);
		
		//找子集
		$cids = $this->getSon($cid);
//		p($cids);
        //在商品表中通过类ID找数据
        $gids = Db::table('goods')->where("goods_category_id in (".(implode(",", $cids)).")")->lists('goods_id');
		//p($gids);
		if($gids)
		{
			$goodsAttr = Db::table('goods_attr')->where("goodsattr_goods_id in (".(implode(",", $gids)).")")->groupBy('goods_value')->get();
			//p($goodsAttr);
			//重组数组将类型属性id相同的放一起
			$tempAttr = array();
			foreach($goodsAttr as $k=>$v)
			{
				$tempAttr[$v['goodsattr_typeattr_id']][] = $v;
			}
			//p($tempAttr);
			$finalTempAttr = array();
			foreach($tempAttr as $k=>$v)
			{
				$finalTempAttr[] = array(
				 "name"=>Db::table("typeattr")->where('typeattr_id',$k)->pluck('typeattr_name'),
				 "value"=>$v,
				);
			}
//			p($finalTempAttr);
            View::with('finalTempAttr',$finalTempAttr);
            //进行筛选
            //设置param参数,用0填充
            $param = isset($_GET['param'])?explode("_", $_GET['param']):array_fill(0, count($finalTempAttr), 0);
			View::with('param',$param);
			foreach($param as $k=>$v)
			{
				//没有筛选不进行处理
				if($v)
				{
					//在上边groupby会有遗漏
					////如果有筛选的时候，就是param参数不为零的进行处理
					$filterGids[] = Db::table('goods_attr ga1')->join('goods_attr ga2','ga1.goods_value','=','ga2.goods_value')->where('ga1.goodsattr_id',$v)->field("ga2.goodsattr_goods_id")->get();					
				}
			}
			if(isset($filterGids)){
				foreach($filterGids as $key=>$value)
					{
						foreach($value as $kk=>$vv)
						{
							$filterGids[$key][$kk] = $vv['goodsattr_goods_id'];
						}
					}
				//求交集
				$finalGids = $filterGids[0];
				foreach($filterGids as $k=>$v)
				{
					$finalGids = array_intersect($finalGids, $v);
				}
				$finalGids = array_intersect($finalGids,$gids);
			}else{
				//如果没有筛选的时候
				$finalGids = $gids;
			}		
		}else{
			$finalGids = array();
		}
//		p($finalGids);
		//循环商品id
		if(!empty($finalGids))
		{
			foreach($finalGids as $k=>$v)
		    {
			   $goodsData[] = Db::table('goods')->where('goods_id',$v)->first();
		    }	
		}else{
			$goodsData = array();
		}
		
//		p($goodsData);
		View::with('goodsData',$goodsData);
		View::make();
	}
	//找父集
	public function getFather($data,$cid)
	{
	   static $temp = array();
	   foreach($data as $k=>$v)
	   {
	   	if($cid == $v['category_id'])
		{
			$temp[] = $v;
			$this->getFather($data, $v['category_pid']);
		}
	   }
	   return $temp;
	}
	//把自己压入子集
	public function getSon($cid)
	{
		$cids = $this->getSonCid(Db::table('category')->get(),$cid);
		$cids[] = $cid;
		return $cids;
	}
	//找子集
	public function getSonCid($data,$cid)
	{
		static $temp = array();
		foreach($data as $k=>$v)
		{
			if($v['category_pid']==$cid)
			{
				$temp[] = $v['category_cid'];
				$this->getSonCid($data, $v['category_cid']);
			}			
		}
		return $temp;
	}
}
